home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / impression / ssrand.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  211 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    ssrand - 
  19.  *        Add noise to a sample set
  20.  *
  21.  *            Paul Haeberli - 1988
  22.  */
  23. #include "stdio.h"
  24. #include "ss.h"
  25. #include "math.h"
  26.  
  27. #define COLOR        1
  28. #define DIRECTION    2
  29. #define POSITION    4
  30. #define SIZE        8
  31.  
  32. main(argc,argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     sampleset *iss, *oss;
  37.     float mag;
  38.     int i, mode;
  39.  
  40.     if( argc<3 ) {
  41.     fprintf(stderr,"usage: ssrand in.ss out.ss mag [-c -d -p -s]\n");
  42.     exit(1);
  43.     } 
  44.     srand(getpid());
  45.     mode = 0;
  46.     if(argc>3) {
  47.     mag = atof(argv[3]);
  48.     for(i=4; i<argc; i++) {
  49.         if(strcmp(argv[i],"-c") == 0)
  50.         mode |= COLOR;
  51.         if(strcmp(argv[i],"-d") == 0)
  52.         mode |= DIRECTION;
  53.         if(strcmp(argv[i],"-p") == 0)
  54.         mode |= POSITION;
  55.         if(strcmp(argv[i],"-s") == 0)
  56.         mode |= SIZE;
  57.     }
  58.     }
  59.     iss = ssfromfile(argv[1]);
  60.     oss = ssnew();
  61.     if(mode == 0)
  62.     ssrandord(iss,oss);
  63.     else
  64.     dossrand(iss,oss,mode,mag);
  65.     sstofile(argv[2],oss);
  66. }
  67.  
  68. ssrandord(iss,oss)
  69. sampleset *iss, *oss; 
  70. {
  71.     sample *tab[256];
  72.     sample *s, *ns;
  73.     int i;
  74.  
  75.     for(i=0; i<256; i++) 
  76.     tab[i] = 0;
  77.     s = iss->head;
  78.     while(s) {
  79.     ns = sclone(s);
  80.     i = rand()%256;
  81.     ns->next = tab[i];
  82.     tab[i] = ns;
  83.     s = s->next;
  84.     }
  85.     for(i=0; i<256; i++) {
  86.     while(tab[i]) {
  87.         ns = tab[i];
  88.         tab[i] = ns->next;
  89.         addsample(oss,ns);
  90.     }
  91.     }
  92. }
  93.  
  94. dossrand(iss,oss,mode,mag)
  95. sampleset *iss, *oss; 
  96. int mode;
  97. float mag;
  98. {
  99.     sample *s, *ns;
  100.     int imag, bimag;
  101.  
  102.     imag = 255*mag;
  103.     bimag = 32000*mag;
  104.     s = iss->head;
  105.     while(s) {
  106.     ns = sclone(s);
  107.     if(mode & COLOR) {
  108.         modifycolor(ns,imag);
  109.     }
  110.     if(mode & DIRECTION) {
  111.         ns->at1 += mynoise(imag);
  112.     }
  113.     if(mode & POSITION) {
  114.         ns->xpos += mynoise(bimag);
  115.         ns->ypos += mynoise(bimag);
  116.     }
  117.     if(mode & SIZE) {
  118.         ns->at0 = (ns->at0*(255+mynoise(imag)))/255;
  119.     }
  120.     addsample(oss,ns);
  121.     s = s->next;
  122.     }
  123. }
  124.  
  125. modifycolor(ns,imag)
  126. sample *ns;
  127. int imag;
  128. {
  129.     float fr, fg, fb;
  130.     float h, s, v;
  131.     int ih, is, iv;
  132.  
  133. #define MOD_RGB
  134. #ifdef MOD_RGB
  135.     ns->r = addnoise(ns->r,imag);
  136.     ns->g = addnoise(ns->g,imag);
  137.     ns->b = addnoise(ns->b,imag);
  138. #endif
  139.  
  140. #ifdef MOD_HUE
  141.     fr = ns->r/255.0;
  142.     fg = ns->g/255.0;
  143.     fb = ns->b/255.0;
  144.     rgb_to_hsv(fr,fg,fb,&h,&s,&v);
  145.     ih  = 255*h;
  146.     ih = ih + mynoise(imag);
  147.     if(ih<0)
  148.        ih += 256;
  149.     if(ih>255)
  150.        ih -= 256;
  151.     if(ih<0)
  152.        ih = 0;
  153.     if(ih>255)
  154.        ih = 255;
  155.     h = ih/255.0;
  156.     hsv_to_rgb(h,s,v,&fr,&fg,&fb);
  157.     ns->r = 255*fr;
  158.     ns->g = 255*fg;
  159.     ns->b = 255*fb;
  160. #endif
  161.  
  162. #ifdef MOD_VALUE
  163.     fr = ns->r/255.0;
  164.     fg = ns->g/255.0;
  165.     fb = ns->b/255.0;
  166.     rgb_to_hsv(fr,fg,fb,&h,&s,&v);
  167.     iv  = 255*v;
  168.     iv = addnoise(iv,imag);
  169.     v = iv/255.0;
  170.     hsv_to_rgb(h,s,v,&fr,&fg,&fb);
  171.     ns->r = 255*fr;
  172.     ns->g = 255*fg;
  173.     ns->b = 255*fb;
  174. #endif
  175.  
  176. #ifdef MOD_SAT
  177.     fr = ns->r/255.0;
  178.     fg = ns->g/255.0;
  179.     fb = ns->b/255.0;
  180.     rgb_to_hsv(fr,fg,fb,&h,&s,&v);
  181.     is  = 255*s;
  182.     is = addnoise(is,imag);
  183.     s = is/255.0;
  184.     hsv_to_rgb(h,s,v,&fr,&fg,&fb);
  185.     ns->r = 255*fr;
  186.     ns->g = 255*fg;
  187.     ns->b = 255*fb;
  188. #endif
  189. }
  190.  
  191. mynoise(imag)
  192. int imag;
  193. {
  194.     if(imag<2)
  195.        imag = 2;
  196.     return (rand()%(imag<<1))-imag;
  197. }
  198.  
  199. addnoise(v,amp)
  200. int v, amp;
  201. {
  202.     if(v<amp) 
  203.     amp = v;
  204.     if((255-v)<amp) 
  205.     amp = 255-v;
  206.     if(amp) 
  207.     return v+(rand()%(amp<<1))-amp;
  208.     else
  209.     return v;
  210. }
  211.